import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '../views/layout1/layout.vue' // 首页
import Login from '../views/login/index.vue' // 登录页
import Notfoud from '../views/notfound/notfoud.vue' // 404
import Home from '.././views/home/home.vue'
import Wenda from '.././views/wenda/wenda.vue'
import Shiping from '.././views/shiping/shiping.vue'
import Wode from '.././views/my/my.vue'
import Serch from '.././views/serch/serch.vue'
import Result from '.././views/serch/result.vue'
import Article from '.././views/article'
import store from '@/store/index'
import Edit from '@/views/my/edit.vue'
import Xiaozhi from '@/views/xiaozhi/index.vue'
Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: '/home',
        component: Home,
        meta: {
          iskeepalive: true
        }
      },
      {
        path: '/wenda',
        component: Wenda
      },
      {
        path: '/shiping',
        component: Shiping
      },
      {
        path: 'wode',
        component: Wode
      }
    ]
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/notfoud',
    component: Notfoud
  },
  {
    path: '/search',
    component: Serch
  },
  {
    path: '/search/result',
    component: Result
  },
  {
    path: '/article/:id',
    component: Article
  },
  {
    path: '/edit',
    component: Edit
  },
  {
    path: '/xiaozhi',
    component: Xiaozhi
  }
]

const router = new VueRouter({
  routes
})
const arr = ['/wode', '/edit']
router.beforeEach((to, from, next) => {
  console.log(to)
  const token = store.state.user.token.token
  next()
  if (arr.some((item) => item === to.fullPat)) {
    if (!token) {
      next('/login')
    }
  }

  console.log(token)
})

export default router
